package com.yonyou.pmclouds.experuser.service.rmiimpl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.conf.ExperServerProperties;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.experuser.entity.ExperUserVO;
import com.yonyou.pmclouds.experuser.mapper.ExperUserMapper;
import com.yonyou.pmclouds.experuser.rmiitf.ExperUserRemoteMobService;

import lombok.Data;

@Data
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ExperUserRemoteMobService.class)
public class ExperUserRemoteMobServiceImpl implements ExperUserRemoteMobService {

	@Autowired
    private ExperUserMapper experUserMapper;
	@Autowired
	private ExperServerProperties experServerProperties;
	
	@Override
	public ExperUserVO addExperUser(ExperUserVO experUserVO) throws BusinessException {
		
		List<ExperUserVO> experUserList = queryExperUserByUserId(experUserVO.getUserid());

		if(experUserList != null && experUserList.size() > 0){
			return experUserList.get(0);
		}
		
		SingleInsertBpTemplate<ExperUserVO> bp = new SingleInsertBpTemplate<>(experUserMapper);
		bp.addBeforeProcessor(new AuditInfoInsertProcessor<ExperUserVO>());
		bp.insert(experUserVO);
		
		return experUserVO;
	}

	@Override
	public List<ExperUserVO> queryExperUserByUserId(String userId) throws BusinessException {
		return experUserMapper.queryExperUserByUserId(userId);
	}

	@Override
	public List<ExperUserVO> queryOvertimeUser() throws BusinessException {
		
		Date currDate = new Date();
		long before =Integer.valueOf(experServerProperties.getExper_period()) *24*60*60*1000;
		Date startDate = new Date(currDate.getTime()- before);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String startFormatDate = sdf.format(startDate);
		
		return experUserMapper.queryOvertimeUser(startFormatDate);
	}

}
